package com.hermeneute.fsa.editors;

import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.ITextDoubleClickStrategy;
import org.eclipse.jface.text.TextAttribute;
import org.eclipse.jface.text.contentassist.ContentAssistant;
import org.eclipse.jface.text.contentassist.IContentAssistant;
import org.eclipse.jface.text.presentation.IPresentationReconciler;
import org.eclipse.jface.text.presentation.PresentationReconciler;
import org.eclipse.jface.text.rules.DefaultDamagerRepairer;
import org.eclipse.jface.text.rules.Token;
import org.eclipse.jface.text.source.ISourceViewer;
import org.eclipse.jface.text.source.SourceViewerConfiguration;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.FontMetrics;

import com.hermeneute.fsa.editors.contentassist.FSAContentAssistProcessor;
import com.hermeneute.fsa.editors.scanners.FSAPartitionScanner;
import com.hermeneute.fsa.editors.scanners.FSAScanner;
import com.hermeneute.fsa.editors.scanners.FSATagScanner;

public class FSAConfiguration extends SourceViewerConfiguration {
	private FSADoubleClickStrategy doubleClickStrategy;
	private FSAScanner scanner=null;
	private FSAScanner autScanner=null;
	private FSAScanner prodScanner=null;
	private FSAScanner stringScanner=null;
	private FSAScanner commentScanner=null;
	private ColorManager colorManager=null;
	private FontMetrics fontManager=null;

	public FSAConfiguration(ColorManager colorManager) {
		this.colorManager = colorManager;
		
	}
	public String[] getConfiguredContentTypes(ISourceViewer sourceViewer) {
		return new String[] {
			IDocument.DEFAULT_CONTENT_TYPE,
			FSAPartitionScanner.FSA_COMMENT,
			FSAPartitionScanner.FSA_AUTOMATON };
	}
	public ITextDoubleClickStrategy getDoubleClickStrategy(
		ISourceViewer sourceViewer,
		String contentType) {
		if (doubleClickStrategy == null)
			doubleClickStrategy = new FSADoubleClickStrategy();
		return doubleClickStrategy;
	}

	protected FSAScanner getFSAScanner() {
		if (scanner == null) {
			scanner = new FSAScanner(colorManager);
			scanner.setDefaultReturnToken(
				new Token(
					new TextAttribute(
						colorManager.getColor(IFSAColorConstants.FSA_DEFAULT))));
		}
		return scanner;
	}

	protected FSAScanner getFSAAutScanner() {
		if (autScanner == null) {
			autScanner = new FSAScanner(colorManager);
			autScanner.setDefaultReturnToken(
				new Token(
					new TextAttribute(
						colorManager.getColor(IFSAColorConstants.FSA),
//						colorManager.getColor(IFSAColorConstants.WHITE),
						null,
						SWT.BOLD)));
		}
		return autScanner;
	}

	protected FSAScanner getFSAProdScanner() {
		if (prodScanner == null) {
			prodScanner = new FSAScanner(colorManager);
			prodScanner.setDefaultReturnToken(
				new Token(
					new TextAttribute(
						colorManager.getColor(IFSAColorConstants.FSA_PRODUCTION))));
		}
		return prodScanner;
	}

	protected FSAScanner getFSAStringScanner() {
		if (stringScanner == null) {
			stringScanner = new FSAScanner(colorManager);
			stringScanner.setDefaultReturnToken(
				new Token(
					new TextAttribute(
						colorManager.getColor(IFSAColorConstants.FSA_DEFAULT),
//						colorManager.getColor(IFSAColorConstants.WHITE),
						null,
						SWT.BOLD)));
		}
		return stringScanner;
	}

	protected FSAScanner getFSACommentScanner() {
		if (commentScanner == null) {
			commentScanner = new FSAScanner(colorManager);
			commentScanner.setDefaultReturnToken(
				new Token(
					new TextAttribute(
						colorManager.getColor(IFSAColorConstants.FSA_COMMENT))));
		}
		return commentScanner;
	}

	
	@Override
	public IContentAssistant getContentAssistant(ISourceViewer sourceViewer) {
    	ContentAssistant assistant = new ContentAssistant();
        assistant.setContentAssistProcessor(new FSAContentAssistProcessor(),IDocument.DEFAULT_CONTENT_TYPE);
        assistant.setContentAssistProcessor(new FSAContentAssistProcessor(),FSAPartitionScanner.FSA_AUTOMATON);
        assistant.enableAutoActivation(true);
        assistant.setAutoActivationDelay(500);
        assistant.setProposalPopupOrientation(IContentAssistant.CONTEXT_INFO_BELOW);
        assistant.setContextInformationPopupOrientation(IContentAssistant.CONTEXT_INFO_BELOW);
        return assistant;
	}
	
	public IPresentationReconciler getPresentationReconciler(ISourceViewer sourceViewer) {
		PresentationReconciler reconciler = new PresentationReconciler();

		DefaultDamagerRepairer dr =
			new DefaultDamagerRepairer(getFSAAutScanner());
		reconciler.setDamager(dr, FSAPartitionScanner.FSA_AUTOMATON);
		reconciler.setRepairer(dr, FSAPartitionScanner.FSA_AUTOMATON);

		dr = new DefaultDamagerRepairer(getFSAProdScanner());
		reconciler.setDamager(dr, FSAPartitionScanner.FSA_EPRODUCTION);
		reconciler.setRepairer(dr, FSAPartitionScanner.FSA_EPRODUCTION);

		dr = new DefaultDamagerRepairer(getFSAStringScanner());
		reconciler.setDamager(dr, FSAPartitionScanner.FSA_STRING);
		reconciler.setRepairer(dr, FSAPartitionScanner.FSA_STRING);

		dr = new DefaultDamagerRepairer(getFSACommentScanner());
		reconciler.setDamager(dr, FSAPartitionScanner.FSA_COMMENT);
		reconciler.setRepairer(dr, FSAPartitionScanner.FSA_COMMENT);

		dr = new DefaultDamagerRepairer(getFSAScanner());
		reconciler.setDamager(dr, IDocument.DEFAULT_CONTENT_TYPE);
		reconciler.setRepairer(dr, IDocument.DEFAULT_CONTENT_TYPE);


		return reconciler;
	}
}